const timestampFormat = (timestamp, format = 'second') => {
    let date = new Date(timestamp * 1000);
    let YY = date.getFullYear();
    if (format === 'year' || format === '年') {
        return YY;
    }
    let MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    if (format === 'month' || format === '月') {
        return YY + '-' + MM;
    }
    let DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
    if (format === 'day' || format === '日') {
        return YY + '-' + MM + '-' + DD;
    }
    let hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours());
    if (format === 'hour') {
        return YY + '-' + MM + '-' + DD + " " + hh;
    }
    let mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
    if (format === 'minute') {
        return YY + '-' + MM + '-' + DD + " " + hh + ":" + mm;
    }
    let ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
    if (format === 'second') {
        return YY + '-' + MM + '-' + DD + " " + hh + ":" + mm + ":" + ss;
    }
    return '';
}

Date.prototype.Format = function (fmt) { //author: meizz
    const o = {
        "M+": this.getMonth() + 1,                 //月份
        "d+": this.getDate(),                    //日
        "H+": this.getHours(),                   //小时
        "m+": this.getMinutes(),                 //分
        "s+": this.getSeconds(),                 //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (const k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

export default {
    timestampFormat
}
